<?xml version="1.0" encoding="iso-8859-1"?>

<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>Class: ActiveBackground::Worker</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />

    <script language="JavaScript" type="text/javascript">
    // <![CDATA[

        function toggleSource( id )
        {
          var elem
          var link

          if( document.getElementById )
          {
            elem = document.getElementById( id )
            link = document.getElementById( "l_" + id )
          }
          else if ( document.all )
          {
            elem = eval( "document.all." + id )
            link = eval( "document.all.l_" + id )
          }
          else
            return false;

          if( elem.style.display == "block" )
          {
            elem.style.display = "none"
            link.innerHTML = "show source"
          }
          else
          {
            elem.style.display = "block"
            link.innerHTML = "hide source"
          }
        }

        function openCode( url )
        {
          window.open( url, "SOURCE_CODE", "width=400,height=400,scrollbars=yes" )
        }
      // ]]>
    </script>

  </head>

  <body>
  <table width="100%" border='0' cellpadding='0' cellspacing='0' class='banner'><tr>
  <td class="file-title"><span class="file-title-prefix">Class</span><br />ActiveBackground::Worker</td>
  <td align="right">
    <table cellspacing=0 cellpadding=2>
      <tr valign="top">
        <td>In:</td>
        <td>

<a href="../../files/vendor/plugins/active_background/tasks/__/lib/active_background/jobs/worker_rb.html">vendor/plugins/active_background/tasks/../lib/active_background/jobs/worker.rb</a>
        </td>
      </tr>
    <tr>
      <td>Parent:</td>
      <td>
        <a href="Job.html">
ActiveBackground::Job
         </a>

     </td>
   </tr>
         </table>
        </td>
        </tr>
      </table>
 <!-- banner header -->

  <div id="bodyContent">
      <div id="content">




  <div class="sectiontitle">Methods</div>
  <ul>
  <li><a href="#M000006">new</a></li>
  <li><a href="#M000007">process_job</a></li>
  <li><a href="#M000008">work</a></li>
  <li><a href="#M000009">work_on</a></li>
  </ul>








<div class="sectiontitle">Public Class methods</div>
<div class="method">
  <div class="title">
    <a name="M000006"></a><b>new</b>(name, options={}, args={})
  </div>
<div class="sourcecode">
  <p class="source-link">[ <a href="javascript:toggleSource('M000006_source')" id="l_M000006_source">show source</a> ]</p>

  <div id="M000006_source" class="dyn-source">
<pre>
   <span class="ruby-comment cmt"># File vendor/plugins/active_background/tasks/../lib/active_background/jobs/worker.rb, line 4</span>
4:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">name</span>, <span class="ruby-identifier">options</span>={}, <span class="ruby-identifier">args</span>={})
5:       <span class="ruby-keyword kw">super</span>(<span class="ruby-identifier">name</span>, <span class="ruby-identifier">options</span>, <span class="ruby-identifier">args</span>)
6:       <span class="ruby-ivar">@worker_threads</span> = <span class="ruby-constant">Hash</span>.<span class="ruby-identifier">new</span>
7:     <span class="ruby-keyword kw">end</span>
</pre>
  </div>
</div>
</div>

<div class="sectiontitle">Public Instance methods</div>
<div class="method">
  <div class="title">
    <a name="M000007"></a><b>process_job</b>()
  </div>
<div class="sourcecode">
  <p class="source-link">[ <a href="javascript:toggleSource('M000007_source')" id="l_M000007_source">show source</a> ]</p>

  <div id="M000007_source" class="dyn-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/plugins/active_background/tasks/../lib/active_background/jobs/worker.rb, line 9</span>
 9:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">process_job</span>()
10:       <span class="ruby-comment cmt"># start working, this method is always run unless termination is called</span>
11:       <span class="ruby-comment cmt"># in one of the methods registerd by before_work</span>
12:       <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">terminated?</span>
13:         <span class="ruby-ivar">@logger</span>.<span class="ruby-identifier">debug</span>(<span class="ruby-value str">'job '</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">key</span> <span class="ruby-operator">+</span> <span class="ruby-value str">' is processing'</span>)
14:         <span class="ruby-identifier">work</span>()
15:       <span class="ruby-keyword kw">end</span>
16:     <span class="ruby-keyword kw">end</span>
</pre>
  </div>
</div>
</div>
<div class="method">
  <div class="title">
    <a name="M000008"></a><b>work</b>()
  </div>
<div class="sourcecode">
  <p class="source-link">[ <a href="javascript:toggleSource('M000008_source')" id="l_M000008_source">show source</a> ]</p>

  <div id="M000008_source" class="dyn-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/plugins/active_background/tasks/../lib/active_background/jobs/worker.rb, line 18</span>
18:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">work</span>()
19:       <span class="ruby-ivar">@logger</span>.<span class="ruby-identifier">warn</span>(<span class="ruby-value str">'You need to implement work or create tasks'</span>)
20:       <span class="ruby-ivar">@logger</span>.<span class="ruby-identifier">warn</span>(<span class="ruby-value str">'trying to execute defined tasks'</span>)
21:       <span class="ruby-identifier">work_on</span> <span class="ruby-identifier">:all</span>
22:     <span class="ruby-keyword kw">end</span>
</pre>
  </div>
</div>
</div>
<div class="method">
  <div class="title">
    <a name="M000009"></a><b>work_on</b>(*tasks)
  </div>
<div class="sourcecode">
  <p class="source-link">[ <a href="javascript:toggleSource('M000009_source')" id="l_M000009_source">show source</a> ]</p>

  <div id="M000009_source" class="dyn-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/plugins/active_background/tasks/../lib/active_background/jobs/worker.rb, line 24</span>
24:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">work_on</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">tasks</span>)
25:       <span class="ruby-comment cmt"># if there are multiple tasks specified</span>
26:       <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">tasks</span>[<span class="ruby-value">0</span>] <span class="ruby-operator">==</span> <span class="ruby-identifier">:all</span>
27:         <span class="ruby-keyword kw">for</span> <span class="ruby-identifier">task</span> <span class="ruby-keyword kw">in</span> <span class="ruby-identifier">tasks</span>
28:           <span class="ruby-comment cmt"># convert the taskname to method names. If a task</span>
29:           <span class="ruby-comment cmt"># is called do_it the method should be task_do_it</span>
30:           <span class="ruby-identifier">task</span> = <span class="ruby-value str">'task_'</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">task</span>.<span class="ruby-identifier">to_s</span>
31:           <span class="ruby-comment cmt"># if there is alreaddy a thread lets skip performing</span>
32:           <span class="ruby-comment cmt"># the task. This prevents multiple execution of tasks</span>
33:           <span class="ruby-keyword kw">unless</span> <span class="ruby-ivar">@worker_threads</span>[<span class="ruby-identifier">task</span>]
34:             <span class="ruby-ivar">@worker_threads</span>[<span class="ruby-identifier">task</span>] = <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">new</span> <span class="ruby-keyword kw">do</span>
35:               <span class="ruby-comment cmt"># perform the task in its own thread</span>
36:               <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">task</span>.<span class="ruby-identifier">to_sym</span>)
37:             <span class="ruby-keyword kw">end</span>
38:           <span class="ruby-keyword kw">end</span>
39:         <span class="ruby-keyword kw">end</span>
40:       <span class="ruby-keyword kw">else</span> <span class="ruby-comment cmt"># work_on :all is called</span>
41:         <span class="ruby-comment cmt"># we only process the public methods available to the class</span>
42:         <span class="ruby-keyword kw">for</span> <span class="ruby-identifier">method</span> <span class="ruby-keyword kw">in</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">public_methods</span>
43:           <span class="ruby-comment cmt"># check if the method begins with task_</span>
44:           <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">method</span>.<span class="ruby-identifier">match</span>(<span class="ruby-value str">'task_'</span>)
45:             <span class="ruby-comment cmt"># if there is already a thread lets skip performing</span>
46:             <span class="ruby-comment cmt"># the task. This prevents multiple execution of tasks</span>
47:             <span class="ruby-keyword kw">unless</span> <span class="ruby-ivar">@worker_threads</span>[<span class="ruby-identifier">method</span>]
48:               <span class="ruby-ivar">@worker_threads</span>[<span class="ruby-identifier">method</span>] = <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">new</span> <span class="ruby-keyword kw">do</span>
49:                 <span class="ruby-comment cmt"># perform the task in its own thread</span>
50:                 <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">method</span>.<span class="ruby-identifier">to_sym</span>)
51:               <span class="ruby-keyword kw">end</span>
52:             <span class="ruby-keyword kw">end</span>
53:           <span class="ruby-keyword kw">end</span>
54:         <span class="ruby-keyword kw">end</span>
55:       <span class="ruby-keyword kw">end</span>
56:       <span class="ruby-comment cmt"># wait till all tasks are finished</span>
57:       <span class="ruby-ivar">@worker_threads</span>.<span class="ruby-identifier">each</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">task</span>, <span class="ruby-identifier">thread</span><span class="ruby-operator">|</span> <span class="ruby-identifier">thread</span>.<span class="ruby-identifier">join</span>}
58:       <span class="ruby-comment cmt"># reset to defaults if all tasks are finished</span>
59:       <span class="ruby-ivar">@worker_threads</span> = <span class="ruby-constant">Hash</span>.<span class="ruby-identifier">new</span>
60:     <span class="ruby-keyword kw">end</span>
</pre>
  </div>
</div>
</div>
</div>

  </div>

    </body>
</html>